﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Extensions
{
	using System.Security.Cryptography;
	using System.Text;

	public static class HashExtension
	{
		/// <summary>
		/// Hashes the value using the specified hash algorithm.
		/// </summary>
		/// <param name="algorithm">The algorithm.</param>
		/// <param name="value">The value.</param>
		/// <returns></returns>
		private static byte[] Hash(HashAlgorithm algorithm, string value)
		{
			return algorithm.ComputeHash(Encoding.UTF8.GetBytes(value));
		}

		/// <summary>
		/// Hashes to sha1.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <returns></returns>
		public static byte[] HashToSha1(this string value)
		{
			return Hash(SHA1.Create(), value);
		}

		/// <summary>
		/// Hashes to sha1.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <param name="salt">The salt.</param>
		/// <returns></returns>
		public static byte[] HashToSha1(this string value, string salt)
		{
			return Hash(SHA1.Create(), string.Concat(value, salt));
		}

		/// <summary>
		/// Hashes to sha256.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <returns></returns>
		public static byte[] HashToSha256(this string value)
		{
			return Hash(SHA256.Create(), value);
		}

		/// <summary>
		/// Hashes to sha256.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <param name="salt">The salt.</param>
		/// <returns></returns>
		public static byte[] HashToSha256(this string value, string salt)
		{
			return Hash(SHA256.Create(), string.Concat(value, salt));
		}

		/// <summary>
		/// Hashes to sha384.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <returns></returns>
		public static byte[] HashToSha384(this string value)
		{
			return Hash(SHA384.Create(), value);
		}

		/// <summary>
		/// Hashes to sha384.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <param name="salt">The salt.</param>
		/// <returns></returns>
		public static byte[] HashToSha384(this string value, string salt)
		{
			return Hash(SHA384.Create(), string.Concat(value, salt));
		}

		/// <summary>
		/// Hashes to sha512.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <returns></returns>
		public static byte[] HashToSha512(this string value)
		{
			return Hash(SHA512.Create(), value);
		}

		/// <summary>
		/// Hashes to sha512.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <param name="salt">The salt.</param>
		/// <returns></returns>
		public static byte[] HashToSha512(this string value, string salt)
		{
			return Hash(SHA512.Create(), string.Concat(value, salt));
		}

		/// <summary>
		/// Hashes to MD5.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <returns></returns>
		public static byte[] HashToMd5(this string value)
		{
			return Hash(MD5.Create(), value);
		}

		/// <summary>
		/// Hashes to MD5.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <param name="salt">The salt.</param>
		/// <returns></returns>
		public static byte[] HashToMd5(this string value, string salt)
		{
			return Hash(MD5.Create(), string.Concat(value, salt));
		}
	}
}